//
//  MJVideoView.m
//  MJVideoPlayer
//
//  Created by 梅杰 on 16/8/13.
//  Copyright © 2016年 梅杰. All rights reserved.
//

#import "MJVideoView.h"
#import "UIImage+MJPlayer.h"
#import "Masonry.h"
@interface MJVideoView ()

@property (nonatomic,weak) UIImageView *thumbImgView;
@property (nonatomic,weak) UIButton *playBtn;
@property (nonatomic,weak) UILabel *nameLbl;
@end

@implementation MJVideoView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}
//用来更新约束
- (void)updateConstraints{
    __weak typeof(self) __wakeSelf = self;
    
    [self.thumbImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.insets = UIEdgeInsetsMake(0, 0, 0, 0);
    }];
    [self.playBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(__wakeSelf);
        make.size.mas_equalTo(CGSizeMake(64, 64));
    }];
    [self.nameLbl mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(self.frame.size.width, 15));
        make.bottom.equalTo(self);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    [super updateConstraints];
}



- (void)layoutSubviews{
    [super layoutSubviews];
//    [self updateConstraints];
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
//    [self layoutIfNeeded];
}

- (void)setAVideo:(MJVideo *)aVideo{
    _aVideo = aVideo;
//    dispatch_queue_t queue = dispatch_queue_create("setThumbQuene", DISPATCH_QUEUE_CONCURRENT);
//    dispatch_async(queue, ^{
//        UIImage *thumbImg = [UIImage thumbnailImageForVideo:self.aVideo.url];
//        if (thumbImg != nil) {
//            self.thumbImgView.image = thumbImg;
//        }
//    });
    if(_aVideo.imageUrlString){
        self.thumbImgView.image = [UIImage imageWithContentsOfFile:self.aVideo.imageUrlString];
//        self.thumbImgView.image = [UIImage imageNamed:@"test"];
    }
    if (_aVideo.name) {
        self.nameLbl.text = _aVideo.name;
    }
    
}

//加载视频图片
- (void) setup {
    //视频的缩略图
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [self.contentView addSubview:imageView];
    self.thumbImgView = imageView;
    self.thumbImgView.userInteractionEnabled = NO;
//    //给图片加上阴影
    CALayer *layer = imageView.layer;
//    layer.shadowOffset = CGSizeMake(5, 5);
//    layer.shadowRadius = 2;
//    layer.shadowOpacity = 1;
//    layer.shadowColor = [UIColor brownColor].CGColor;
    layer.borderWidth = 5;
    
    layer.borderColor = [UIColor blackColor].CGColor;
    
    
    UIButton *playBtn = [[UIButton alloc] init];
    [self.contentView addSubview:playBtn];
    [playBtn setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    self.playBtn = playBtn;
    playBtn.userInteractionEnabled = NO;
    
//    __weak typeof(self) __wakeSelf = self;
//    [self.thumbImgView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.edges.insets = UIEdgeInsetsMake(0, 0, 0, 0);
//    }];
//    [self.playBtn mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(__wakeSelf);
//        make.size.mas_equalTo(CGSizeMake(64, 64));
//    }];
    UILabel *nameLbl = [[UILabel alloc] init];
    [self addSubview:nameLbl];
    self.nameLbl = nameLbl;
    nameLbl.font = [UIFont systemFontOfSize:15];
    nameLbl.backgroundColor = [UIColor blackColor];
    nameLbl.textColor = [UIColor lightTextColor];
    nameLbl.textAlignment = NSTextAlignmentCenter;
}


@end
